package com.css.modules.elasticSearch.controller;

import com.css.common.utils.FileUtils;
import com.css.common.utils.R;
import com.css.modules.elasticSearch.entity.LawRepoEntity;
import com.css.modules.elasticSearch.page.Page;
import com.css.modules.elasticSearch.service.EsSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * elasticsearch 搜索
 * @author zhoudong
 * @version 0.1
 * @date 2018/12/13 15:09
 */
@RestController
@RequestMapping("/lawRepo/elasticSearch")
@Api(value = "/elasticSearch", description = "ES检索", tags = "ES检索")
public class EsSearchController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private EsSearchService esSearchService;

    @Autowired
    FileUtils fileUtils;

    /**
     * 新增 / 修改索引
     * @return
     */
    @ApiOperation(value = "新增/修改索引", produces = "application/json")
    @RequestMapping(value = "/save" ,method = RequestMethod.POST)
    public R add(@RequestBody LawRepoEntity lawRepoEntity) {
        esSearchService.save(lawRepoEntity);
        return R.ok();
    }

    /**
     * 删除索引
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    @ApiOperation(value = "删除索引", produces = "application/json")
    public R delete(@PathVariable String id) {
        esSearchService.delete(id);
        return R.ok();
    }

    /**
     * 清空索引
     * @return
     */
    @RequestMapping(value = "/delete_all",method = RequestMethod.POST)
    @ApiOperation(value = "清空索引", produces = "application/json")
    public R deleteAll(@PathVariable String id) {
        esSearchService.deleteAll();
        return R.ok();
    }

    /**
     * 根据ID获取
     * @return
     */
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "根据ID获取", produces = "application/json")
    public LawRepoEntity getById(@PathVariable String id){
        return esSearchService.getById(id);
    }

    /**
     * 根据获取全部
     * @return
     */
    @RequestMapping(value = "/get_all",method = RequestMethod.GET)
    @ApiOperation(value = "根据获取全部", produces = "application/json")
    public List<LawRepoEntity> getAll(){
        return esSearchService.getAll();
    }

    /**
     * 搜索
     * @param keyword
     * @return
     */
    @RequestMapping(value = "/query/{keyword}",method = RequestMethod.GET)
    @ApiOperation(value = "搜索", produces = "application/json")
    public List<LawRepoEntity> query(@PathVariable String keyword){
        return esSearchService.query(keyword, LawRepoEntity.class);
    }



    /**
     * 搜索，命中关键字高亮
     * http://localhost:8080/query_hit?keyword=无印良品荣耀&indexName=orders&fields=productName,productDesc
     * @param keyword   关键字
     * @param indexName 索引库名称
     * @param fields    搜索字段名称，多个以“，”分割
     * @return
     */
    @RequestMapping(value = "/query_hit",method = RequestMethod.GET)
    @ApiOperation(value = "搜索.命中关键字高亮", produces = "application/json")
    public List<Map<String,Object>> queryHit(@RequestParam String keyword, @RequestParam String indexName, @RequestParam String fields){
        String[] fieldNames = {};
        if(fields.contains(",")) fieldNames = fields.split(",");
        else fieldNames[0] = fields;
        return esSearchService.queryHit(keyword,indexName,fieldNames);
    }

    /**
     * 搜索，命中关键字高亮
     * http://localhost:8080/query_hit_page?keyword=无印良品荣耀&indexName=orders&fields=productName,productDesc&pageNo=1&pageSize=1
     * @param pageNo    当前页
     * @param pageSize  每页显示的数据条数
     * @param keyword   关键字
     * @param indexName 索引库名称
     * @param fields    搜索字段名称，多个以“，”分割
     * @return
     */
    @RequestMapping(value = "/query_hit_page",method = RequestMethod.GET)
    @ApiOperation(value = "搜索.命中关键字高亮", produces = "application/json")
    public Page<Map<String,Object>> queryHitByPage(@RequestParam int pageNo,@RequestParam int pageSize
            ,@RequestParam String keyword, @RequestParam String indexName, @RequestParam String fields){
        String[] fieldNames = {};
        if(fields.contains(",")) fieldNames = fields.split(",");
        else fieldNames[0] = fields;
        return esSearchService.queryHitByPage(pageNo,pageSize,keyword,indexName,fieldNames);
    }

    /**
     * 删除索引库
     * @param indexName
     * @return
     */
    @RequestMapping(value = "/delete_index/{indexName}",method = RequestMethod.POST)
    @ApiOperation(value = "删除索引库", produces = "application/json")
    public R deleteIndex(@PathVariable String indexName){
        esSearchService.deleteIndex(indexName);
        return R.ok();
    }
    /**
     * 精确搜索，命中关键字高亮
     */
    @RequestMapping(value = "/exquery_hit",method = RequestMethod.POST)
    @ApiOperation(value = "精确搜索.命中关键字高亮", produces = "application/json")
    public List<Map<String,Object>> exQueryHit(@RequestBody Map<String , Object> params){
        return esSearchService.exQueryHit(params,"lawrepo");
    }

    @RequestMapping(value = "/fuzzyquery_hit",method = RequestMethod.POST)
    @ApiOperation(value = "模糊搜索.命中分词高亮", produces = "application/json")
    public List<Map<String,Object>> fuzzyQueryHit(@RequestBody Map<String , Object> params){
        return esSearchService.fuzzyQueryHit(params,"lawrepo");
    }
}
